<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/5/20
 * Time: 17:03
 */
namespace app\admin\controller;
use think\Db;
class Curriculum extends Base {

    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            //活动开始日期
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['n_start_time'] = array('>=',$params['add_start']);
                $where['n_end_time'] = array('<=',$params['add_end']);
            }
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] == ''){
                $where['n_start_time'] = array('>=',$params['add_start']);
            }if(isset($params['add_start']) && $params['add_start'] == '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['n_end_time'] = array('<=',$params['add_end']);
            }

            //报名开始日期
            if(isset($params['add_start1']) && $params['add_start1'] != '' && isset($params['add_end1']) && $params['add_end1'] != ''){
                $where['n_apply_start_time'] = array('>=',$params['add_start1']);
                $where['n_apply_end_time'] = array('<=',$params['add_end1']);
            }
            if(isset($params['add_start1']) && $params['add_start1'] != '' && isset($params['add_end1']) && $params['add_end1'] == ''){
                $where['n_apply_start_time'] = array('>=',$params['add_start1']);
            }if(isset($params['add_start1']) && $params['add_start1'] == '' && isset($params['add_end1']) && $params['add_end1'] != ''){
                $where['n_apply_end_time'] = array('<=',$params['add_end1']);
            }
            //发布日期
            if(isset($params['add_start2']) && $params['add_start2'] != '' && isset($params['add_end2']) && $params['add_end2'] != ''){
                $where['n_time'] = ['between', [$params['add_start2'], $params['add_end2']]];
            }
            if(isset($params['add_start2']) && $params['add_start2'] != '' && isset($params['add_end2']) && $params['add_end2'] == ''){
                $where['n_time'] = ['>=', $params['add_start2']];
            }if(isset($params['add_start2']) && $params['add_start2'] == '' && isset($params['add_end2']) && $params['add_end2'] != ''){
                $where['n_time'] = ['<=', $params['add_end2']];
            }
            //状态
            if(isset($params['n_status']) && $params['n_status'] != ''){
                $where['n_activity_status'] = $params['n_status'];
            }
            if(isset($params['n_status1']) && $params['n_status1'] != ''){
                $where['n_sign_status'] = $params['n_status1'];
            }
            if(isset($params['n_status2']) && $params['n_status2'] != ''){
                $where['n_full_status'] = $params['n_status2'];
            }
            //通知标题
//            if(isset($params['n_title']) && $params['n_title'] != ''){
//                $where['n_title'] = ['like', '%' . $params['n_title'] . '%'];
//            }

            //使用范围
            if(isset($params['n_school']) && $params['n_school'] != ''){
                $n_school = ',' . $params['n_school'] . ',';
                $where['n_school'] = ['like', '%' . $n_school . '%'];
            }
            $where['n_del'] = 1;
            $where['n_type'] = 1;

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('notice')->where($where)->count();// 获取总条数
            $lists = Db::name('notice')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('n_time desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                $vo['time'] = $vo['n_start_time'].' 至 '.$vo['n_end_time'];
                $vo['time1'] = $vo['n_apply_start_time'].' 至 '.$vo['n_apply_end_time'];
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $school = Db::table('up_school')
            ->where('school_status', 1)
            ->where('school_del', 1)
            ->field('school_id, school_name')
            ->select();
        $this->assign([
            'school' => $school
        ]);
        if(session('?curriculum_id')){
            session('curriculum_id',null);
        }
        return $this->fetch();
    }

    /**
     * 批量删除通知
     * @return \think\response\Json
     */
    public function payfeesDel(){
        $ids = input('ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择要删除的通知']);
        }
        $result = Db::table('up_notice')->where('n_id', 'IN', $ids)->setField('n_del', 2);
        if($result){
            return json(['code' => '200', 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    /**
     * 批量修改状态
     * @return \think\response\Json
     */
    public function payfeesStatus(){
        $ids = input('ids');
        $num = input('num');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择需要修改的研学通知']);
        }
        $status = Db::table('up_notice')->where('n_id', 'IN', $ids)->column('n_status');
        if($num == 1){//上架
            if(in_array(1, $status)){
                return json(['code' => 100, 'msg' => '所选通知中含有上架中记录，请重新选择']);
            }
            $msg = '上架成功';
        }else{//下架
            if(in_array(2, $status)){
                return json(['code' => 100, 'msg' => '所选通知中含有已下架记录，请重新选择']);
            }
            $msg = '下架成功';
        }
        $res = Db::table('up_notice')->where('n_id', 'IN', $ids)->setField('n_status', $num);
        if($res){
            return json(['code' => 200, 'msg' => $msg]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    //添加
    public function add(){
        if(request()->isPost()){
            $time = date('Y-m-d H:i:s');
            $n_title = input('param.n_title');
            $num = input('param.num');//活动人数
            $money = input('param.money');//报名费用
            $start = input('param.start');//报名开始时间
            $end = input('param.end');//报名结束时间
            $start1 = input('param.start1');//活动开始时间
            $end1 = input('param.end1');//活动结束时间
            $n_user = input('param.n_user');//联系人
            $n_phone = input('param.n_phone');//联系电话
            $n_school = ',' . input('n_school') . ',';
            $n_thumb = input('n_thumb');
            $n_img = input('n_img');
            $n_content = input('n_content');
            if(count(explode(',',$n_user))>2){
                return json(['code' => 100, 'msg' => '最多可以输入2个联系人']);
            }
            if(count(explode(',',$n_phone))>2){
                return json(['code' => 100, 'msg' => '最多可以输入2个联系电话']);
            }
            if($num <= 0){
                return json(['code' => 100, 'msg' => '报名人数不能低于0']);
            }
            if(strtotime($end)-strtotime($time) <= 120 || strtotime($end1)-strtotime($time) <= 120){
                return json(['code' => 100, 'msg' => '结束时间不能低于当前时间两分钟']);
            }
            if($end <= $start || $end1 <= $start1){
                return json(['code' => 100, 'msg' => '结束时间不能低于开始时间']);
            }
            $data = [
                'n_type' => 1,
                'n_title' => $n_title,
                'n_thumb' => $n_thumb,
                'n_img' => $n_img,
                'n_content' => $n_content,
                'n_school' => $n_school,
                'n_time' => date('Y-m-d H:i:s'),
                'n_start_time'=>$start1,
                'n_end_time'=>$end1,
                'n_apply_start_time'=>$start,
                'n_apply_end_time'=>$end,
                'n_money'=>$money,
                'n_num'=>$num,
                'n_linkman'=>$n_user,
                'n_phone'=>$n_phone,
            ];
            $res = Db::table('up_notice')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $school = Db::table('up_school')
            ->where('school_status', 1)
            ->where('school_del', 1)
            ->field('school_id, school_name')
            ->select();
        $this->assign([
            'school' => $school
        ]);
        return $this->fetch();
    }
    //基本信息
    public function basic(){
        if(request()->isAjax()){
            $where['a.st_n_id'] = input('param.id');
            $where['a.st_pay_status'] = 2;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('studies')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.st_user_id','left')
                ->where($where)->count();// 获取总条数
            $count2 = Db::name('studies')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.st_user_id','left')
                ->where($where)->column('st_id');// 获取总条数
            $lists = Db::name('studies')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.st_user_id','left')
                ->where($where)
                ->page($Nowpage,$limits)
                ->field('a.*,uuu.user_username,uuu.user_phone')
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['st_pay_type'] == 1){
                    $lists[$key]['st_pay_type'] = '支付宝';
                }else{
                    $lists[$key]['st_pay_type'] = '微信';
                }
               $res =  Db::table('up_grade_class')->where('gs_user_id',$vo['st_user_id'])->find();
                if($res['gs_role']==1){//学生
                    $res1 = Db::table('up_school')->where('school_id',$res['gs_school_id'])->find();
                    $res2 = Db::table('up_grade')->where('grade_id',$res['gs_grade_id'])->find();
                    $res3= Db::table('up_class')->where('class_id',$res['gs_class_id'])->find();
                    $vo['school'] = $res1['school_name'];
                    $vo['grade_class'] = $res2['grade_name'].'-'.$res3['class_name'];
                }elseif ($res['gs_role']==2){//老师
                    $data =  Db::table('up_grade_class')->where('gs_user_id',$vo['st_user_id'])->select();
                    $res8 = array();
                    foreach ($data as $key1=>$vo1){
                        $res4 = Db::table('up_school')->where('school_id',$vo1['gs_school_id'])->find();
                        $res5 = Db::table('up_grade')->where('grade_id',$vo1['gs_grade_id'])->find();
                        $res6= Db::table('up_class')->where('class_id',$vo1['gs_class_id'])->find();
                        $res7 = $res4['school_name'];
                        $res8[] = $res5['grade_name'].'-'.$res6['class_name'];
                    }

                    $vo['school'] = $res7;
                    $vo['grade_class'] = implode(',',$res8);
                }

            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
        $id = input('param.id');
        if($id != ''){
            session('curriculum_id',$id);
        }
        return $this->fetch();
    }

    //查看详情
    public function detail(){
        $id = input('param.n_id');
        $res = Db::table('up_notice')->where('n_id',$id)->find();
        $res['school'] = explode(',',trim($res['n_school'],','));
        $this->assign('res',$res);
        $school = Db::table('up_school')
            ->where('school_status', 1)
            ->where('school_del', 1)
            ->field('school_id, school_name')
            ->select();
        $this->assign([
            'school' => $school
        ]);
        return $this->fetch();
    }

    //编辑
    public function update(){
        if(request()->isPost()){
            $time = date('Y-m-d H:i:s');
            $id = input('param.id');
            $n_title = input('param.n_title');
            $num = input('param.num');//活动人数
            $money = input('param.money');//报名费用
            $start = input('param.start');//报名开始时间
            $end = input('param.end');//报名结束时间
            $start1 = input('param.start1');//活动开始时间
            $end1 = input('param.end1');//活动结束时间
            $n_user = input('param.n_user');//联系人
            $n_phone = input('param.n_phone');//联系电话
            $n_school = ',' . input('n_school') . ',';
            $n_thumb = input('n_thumb');
            $n_img = input('n_img');
            $n_content = input('n_content');
            $d = Db::table('up_notice')->where('n_id',$id)->find();
            if(count(explode(',',$n_user))>2){
                return json(['code' => 100, 'msg' => '最多可以输入2个联系人']);
            }
            if(count(explode(',',$n_phone))>2){
                return json(['code' => 100, 'msg' => '最多可以输入2个联系电话']);
            }
            if($num <= 0 || $num < $d['n_num_already']){
                return json(['code' => 100, 'msg' => '报名人数不能低于0且不得少于已报名人数']);
            }
            if(strtotime($end)-strtotime($time) <= 120 || strtotime($end1)-strtotime($time) <= 120){
                return json(['code' => 100, 'msg' => '结束时间不能低于当前时间两分钟']);
            }
            if($end <= $start || $end1 <= $start1){
                return json(['code' => 100, 'msg' => '结束时间不能低于开始时间']);
            }
            $data = [
                'n_type' => 1,
                'n_title' => $n_title,
                'n_content' => $n_content,
                'n_school' => $n_school,
                'n_start_time'=>$start1,
                'n_end_time'=>$end1,
                'n_apply_start_time'=>$start,
                'n_apply_end_time'=>$end,
                'n_money'=>$money,
                'n_num'=>$num,
                'n_linkman'=>$n_user,
                'n_phone'=>$n_phone,
                //----------
                'n_status'=>1,
                'n_activity_status'=>1,
                'n_sign_status'=>1,
                'n_full_status'=>1,
            ];
            if(!empty($n_img)){
                $data['n_img'] = $n_img;
            }
            if(!empty($n_thumb)){
                $data['n_thumb'] = $n_thumb;
            }
            if($num <= $d['n_num_already']){
                $data['n_full_status'] = 2;
            }
            $res = Db::table('up_notice')->where('n_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('param.n_id');
        $res = Db::table('up_notice')->where('n_id',$id)->find();
        $res['school'] = explode(',',trim($res['n_school'],','));
        $this->assign('res',$res);
        $school = Db::table('up_school')
            ->where('school_status', 1)
            ->where('school_del', 1)
            ->field('school_id, school_name')
            ->select();
        $this->assign([
            'school' => $school
        ]);
        return $this->fetch();
    }

    public function excelIn(){
        $ids = input('param.ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择用户']);
        }

            $where['a.st_id'] = array('IN',$ids);
            $where['a.st_pay_status'] = 2;
            $lists = Db::name('studies')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.st_user_id','left')
                ->where($where)
                ->field('a.*,uuu.user_username,uuu.user_phone')
                ->select();
            foreach ($lists as $key=>&$vo){
                $res =  Db::table('up_grade_class')->where('gs_user_id',$vo['st_user_id'])->find();
                if($res['gs_role']==1){//学生
                    $res1 = Db::table('up_school')->where('school_id',$res['gs_school_id'])->find();
                    $res2 = Db::table('up_grade')->where('grade_id',$res['gs_grade_id'])->find();
                    $res3= Db::table('up_class')->where('class_id',$res['gs_class_id'])->find();
                    $vo['school'] = $res1['school_name'];
                    $vo['grade_class'] = $res2['grade_name'].'-'.$res3['class_name'];
                }elseif ($res['gs_role']==2){//老师
                    $data =  Db::table('up_grade_class')->where('gs_user_id',$vo['st_user_id'])->select();
                    $res8 = array();
                    foreach ($data as $key1=>$vo1){
                        $res4 = Db::table('up_school')->where('school_id',$vo1['gs_school_id'])->find();
                        $res5 = Db::table('up_grade')->where('grade_id',$vo1['gs_grade_id'])->find();
                        $res6= Db::table('up_class')->where('class_id',$vo1['gs_class_id'])->find();
                        $res7 = $res4['school_name'];
                        $res8[] = $res5['grade_name'].'-'.$res6['class_name'];
                    }

                    $vo['school'] = $res7;
                    $vo['grade_class'] = implode(',',$res8);
                }
                $lists[$key]['st_user_code'] = "\t".$vo['st_user_code']."\t";
                unset($lists[$key]['st_id']);
                unset($lists[$key]['st_n_id']);
                unset($lists[$key]['st_pay_status']);
                unset($lists[$key]['st_pay_time']);
            }
        $cellname = [
            ['st_user_id','用户ID',20,'LEFT'],
            ['st_time','报名时间',15,'LEFT'],
            ['st_money','报名费用',15,'LEFT'],
            ['st_user_name','报名用户姓名',15,'LEFT'],
            ['st_user_code','报名身份证号码',15,'LEFT'],
            ['st_user_phone','报名紧急联系电话',15,'LEFT'],
            ['st_urg_name','紧急联系人姓名',15,'LEFT'],
            ['user_username','用户姓名',15,'LEFT'],
            ['user_phone','用户手机号码',15,'LEFT'],
            ['school','所属学校',15,'LEFT'],
            ['grade_class','所属年级-班级',30,'LEFT'],
        ];
        $res = exportExcel('报名信息列表','',$cellname,$lists);
        return json($res);
        }
}